package com.xiaoshuidi.cloud.module.contract.service.fee;

import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.framework.tenant.core.util.TenantUtils;
import com.xiaoshuidi.cloud.module.contract.controller.admin.fee.vo.*;
import com.xiaoshuidi.cloud.module.contract.convert.fee.FeesConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.fee.FeesDO;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.fee.FeesMapper;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.module.contract.enums.ErrorCodeConstants.FEES_EXISTS;
import static com.xiaoshuidi.cloud.module.contract.enums.ErrorCodeConstants.FEES_NOT_EXISTS;

/**
 * 费项配置 Service 实现类
 *
 * @author 合房租赁
 */
@Service
public class FeesServiceImpl implements FeesService {

    @Resource
    private FeesMapper feesMapper;
    @Override
    public Long createFees(FeesCreateReqVO createReqVO) {
        // 插入
        FeesDO fees = FeesConvert.INSTANCE.convert(createReqVO);
        feesMapper.insert(fees);
        // 返回
        return fees.getId();
    }

    @Override
    public void createFees(FeeTypesCreateReqVO createReqVO) {

        List<FeesBaseVO> feesBaseVOList=new ArrayList<>();
        if(createReqVO.getCycle()){
            FeesBaseVO feesBaseVO=new FeesBaseVO();
            feesBaseVO.setFeeTypeId(createReqVO.getId());
            feesBaseVO.setFeeSort(ApiConstants.CYCLE);
            feesBaseVOList.add(feesBaseVO);
        }
        if(createReqVO.getOnce()){
            FeesBaseVO feesBaseVO=new FeesBaseVO();
            feesBaseVO.setFeeTypeId(createReqVO.getId());
            feesBaseVO.setFeeSort(ApiConstants.ONCE);
            feesBaseVOList.add(feesBaseVO);
        }
        if(createReqVO.getDeposit()){
            FeesBaseVO feesBaseVO=new FeesBaseVO();
            feesBaseVO.setFeeTypeId(createReqVO.getId());
            feesBaseVO.setFeeSort(ApiConstants.DEPOSIT);
            feesBaseVOList.add(feesBaseVO);
        }
        //查询费项是否存在
        List<FeesDO> feeTypesByName = feesMapper.selectList(FeesDO::getFeeTypeId,createReqVO.getId());
        if(CollectionUtils.isNotEmpty(feeTypesByName)){
            for (int i = feesBaseVOList.size() - 1; i >= 0; i--) {
                for (FeesDO feesDO : feeTypesByName) {
                    if (feesBaseVOList.get(i).getFeeSort().equals(feesDO.getFeeSort())) {
                        feesBaseVOList.remove(i);
                        break;
                    }
                }
            }
            if (feesBaseVOList.size() == 0){
                throw exception(FEES_EXISTS);
            }
        }
        // 批量插入 费项
        List<FeesDO> fees = FeesConvert.INSTANCE.convertBaseList(feesBaseVOList);
        feesMapper.insertBatch(fees);
    }

    @Override
    public void updateFees(FeesUpdateReqVO updateReqVO) {
        // 校验存在
        validateFeesExists(updateReqVO.getId());
        // 更新
        FeesDO updateObj = FeesConvert.INSTANCE.convert(updateReqVO);
        feesMapper.updateById(updateObj);
    }

    @Override
    public void deleteFees(Integer id) {
        // 校验存在
        validateFeesExists(id);
        // 删除
        feesMapper.deleteById(id);
    }

    private void validateFeesExists(Integer id) {
        if (feesMapper.selectById(id) == null) {
            throw exception(FEES_NOT_EXISTS);
        }
    }

    @Override
    public FeesDO getFees(Integer id) {
        return feesMapper.selectById(id);
    }

    @Override
    public List<FeesDO> getFeesList(Collection<Integer> ids) {
        return feesMapper.selectBatchIds(ids);
    }

    @Override
    public void updateFeesList(Collection<FeeSaveVo> feeSaveVos) {
        feesMapper.updateBatch(FeesConvert.INSTANCE.convertList03(feeSaveVos),feeSaveVos.size());
    }

    @Override
    public List<FeesRespVO> getFeesListAll() {
        Long tenantId = Objects.requireNonNull(SecurityFrameworkUtils.getLoginUser()).getTenantId();
        List<FeesRespVO> feesRespVOS = TenantUtils.executeIgnoreAndReturn(() -> feesMapper.selectFeesListAll(tenantId));
        //判断有没有初始化系统费项
        if (CollectionUtils.isEmpty(feesRespVOS)){
            //初始化系统费项
            initializationSystemFeesData();
            return TenantUtils.executeIgnoreAndReturn(() -> feesMapper.selectFeesListAll(tenantId));
        }
        return feesRespVOS;
    }

    //初始化系统费项
    private void initializationSystemFeesData() {
        Long tenantId = Objects.requireNonNull(SecurityFrameworkUtils.getLoginUser()).getTenantId();
        feesMapper.initializationSystemFeesData(tenantId);
    }

    @Override
    public List<FeesListRespVO> getFeesListByFeeType(String feeSort) {
        Long tenantId = Objects.requireNonNull(SecurityFrameworkUtils.getLoginUser()).getTenantId();
        List<FeesListRespVO> feesListRespVOS = TenantUtils.executeIgnoreAndReturn(() ->
                feesMapper.selectFeesListByFeeType(feeSort, tenantId));
        if (CollectionUtils.isEmpty(feesListRespVOS)){
            //初始化系统费项
            initializationSystemFeesData();
            return TenantUtils.executeIgnoreAndReturn(() ->
                    feesMapper.selectFeesListByFeeType(feeSort, tenantId));
        }
        return feesListRespVOS;
    }

    @Override
    public List<FeesRespVO> getFeesTypeListAll() {
        Long tenantId = Objects.requireNonNull(SecurityFrameworkUtils.getLoginUser()).getTenantId();
        List<FeesRespVO> feesRespVOS = TenantUtils.executeIgnoreAndReturn(() -> feesMapper.selectFeesTypeListAll(tenantId));
        //判断有没有初始化系统费项
        if (CollectionUtils.isEmpty(feesRespVOS)){
            //初始化系统费项
            initializationSystemFeesData();
            return TenantUtils.executeIgnoreAndReturn(() -> feesMapper.selectFeesTypeListAll(tenantId));
        }
        return feesRespVOS;
    }

//    @Override
//    public PageResult<FeesDO> getFeesPage(FeesPageReqVO pageReqVO) {
//        return feesMapper.selectPage(pageReqVO);
//    }
//
//    @Override
//    public List<FeesDO> getFeesList(FeesExportReqVO exportReqVO) {
//        return feesMapper.selectList(exportReqVO);
//    }

}
